<template>
  <div class="strategy-container">
    <el-card shadow="never" class="header-card">
      <div class="page-header">
        <div class="header-content">
          <el-icon class="header-icon" :size="28"><Setting /></el-icon>
          <div class="header-text">
            <h2>盈亏策略</h2>
            <p>配置库存盈亏处理规则</p>
          </div>
        </div>
        <div class="header-actions">
          <el-button type="primary" @click="showCreateDialog = true">
            <el-icon><Plus /></el-icon>
            新建策略
          </el-button>
        </div>
      </div>
    </el-card>

    <!-- 策略列表 -->
    <el-card shadow="never">
      <el-table :data="strategies" stripe border>
        <el-table-column prop="name" label="策略名称" width="200" />
        <el-table-column prop="type" label="策略类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getTypeTag(row.type)">{{ getTypeName(row.type) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="condition" label="触发条件" min-width="250" />
        <el-table-column prop="action" label="处理动作" min-width="200" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-switch v-model="row.status" @change="handleStatusChange(row)" />
          </template>
        </el-table-column>
        <el-table-column prop="priority" label="优先级" width="100" align="center" />
        <el-table-column prop="updateTime" label="更新时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)" size="small">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button link type="danger" @click="handleDelete(row)" size="small">
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新建/编辑策略对话框 -->
    <el-dialog v-model="showCreateDialog" title="配置盈亏策略" width="700px" destroy-on-close>
      <el-form :model="strategyForm" :rules="strategyRules" ref="strategyFormRef" label-width="120px">
        <el-form-item label="策略名称" prop="name">
          <el-input v-model="strategyForm.name" placeholder="请输入策略名称" />
        </el-form-item>
        <el-form-item label="策略类型" prop="type">
          <el-select v-model="strategyForm.type" placeholder="请选择类型" style="width: 100%">
            <el-option label="盘盈处理" value="profit" />
            <el-option label="盘亏处理" value="loss" />
            <el-option label="自动审核" value="auto-approve" />
            <el-option label="异常预警" value="alert" />
          </el-select>
        </el-form-item>
        <el-form-item label="触发条件" prop="condition">
          <el-input v-model="strategyForm.condition" type="textarea" :rows="2" placeholder="例如：盘盈金额 > 1000元" />
        </el-form-item>
        <el-form-item label="处理动作" prop="action">
          <el-input v-model="strategyForm.action" type="textarea" :rows="2" placeholder="例如：自动生成入库单" />
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-input-number v-model="strategyForm.priority" :min="1" :max="99" />
        </el-form-item>
        <el-form-item label="启用状态">
          <el-switch v-model="strategyForm.status" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showCreateDialog = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Setting, Plus, Edit, Delete } from '@element-plus/icons-vue'

const showCreateDialog = ref(false)
const strategyFormRef = ref(null)
const currentStrategy = ref(null)

const strategies = ref([
  {
    id: 1,
    name: '大额盘盈自动入库',
    type: 'profit',
    condition: '盘盈金额 > 5000元',
    action: '自动生成入库单，更新库存',
    status: true,
    priority: 1,
    updateTime: '2024-01-15 10:00:00'
  },
  {
    id: 2,
    name: '盘亏异常预警',
    type: 'alert',
    condition: '盘亏金额 > 10000元',
    action: '发送邮件通知，需人工审核',
    status: true,
    priority: 2,
    updateTime: '2024-01-14 15:00:00'
  },
  {
    id: 3,
    name: '小额差异自动审核',
    type: 'auto-approve',
    condition: '盈亏金额绝对值 < 100元',
    action: '自动审核通过，调整库存',
    status: true,
    priority: 3,
    updateTime: '2024-01-13 09:00:00'
  }
])

const strategyForm = reactive({
  name: '',
  type: '',
  condition: '',
  action: '',
  priority: 10,
  status: true
})

const strategyRules = {
  name: [{ required: true, message: '请输入策略名称', trigger: 'blur' }],
  type: [{ required: true, message: '请选择策略类型', trigger: 'change' }],
  condition: [{ required: true, message: '请输入触发条件', trigger: 'blur' }],
  action: [{ required: true, message: '请输入处理动作', trigger: 'blur' }]
}

const handleStatusChange = (row) => {
  ElMessage.success(`策略已${row.status ? '启用' : '禁用'}`)
}

const handleEdit = (row) => {
  currentStrategy.value = row
  Object.assign(strategyForm, row)
  showCreateDialog.value = true
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该策略吗？', '提示', {
    type: 'warning'
  }).then(() => {
    const index = strategies.value.findIndex(s => s.id === row.id)
    if (index > -1) {
      strategies.value.splice(index, 1)
      ElMessage.success('删除成功')
    }
  }).catch(() => {})
}

const handleSave = () => {
  strategyFormRef.value.validate((valid) => {
    if (valid) {
      if (currentStrategy.value) {
        Object.assign(currentStrategy.value, strategyForm)
        ElMessage.success('更新成功')
      } else {
        strategies.value.unshift({
          id: Date.now(),
          ...strategyForm,
          updateTime: new Date().toLocaleString()
        })
        ElMessage.success('创建成功')
      }
      showCreateDialog.value = false
      currentStrategy.value = null
    }
  })
}

const getTypeName = (type) => {
  const map = {
    profit: '盘盈处理',
    loss: '盘亏处理',
    'auto-approve': '自动审核',
    alert: '异常预警'
  }
  return map[type] || type
}

const getTypeTag = (type) => {
  const map = {
    profit: 'success',
    loss: 'danger',
    'auto-approve': '',
    alert: 'warning'
  }
  return map[type] || ''
}
</script>

<style scoped>
.strategy-container {
  padding: 20px;
}

.header-card {
  margin-bottom: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-icon {
  color: #409EFF;
}

.header-text h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.header-text p {
  margin: 4px 0 0 0;
  font-size: 14px;
  color: #909399;
}
</style>

